package ua.org.enginer.entities {
	import com.adobe.serialization.json.JSONDecoder;
	
	
	[Bindable]
	public class DetailObject  {
		
		public var owner:Object
		
		
		public var id:Number
		public var created_time:Date
		public var updated_time:Date
		public var owner_id:Number;
		public var owner_type:String;
		public var name:String;
		public var value:String;
		
		//public var storableProperties:Array = ['id', 'created_time', 'name', 'value']
		
		public function DetailObject(o:Object=null):void {
			applyData(o)
			//setPropertyIsEnumerable("owner_id", true)
			//setPropertyIsEnumerable("owner_type", true)
		}
		
		public function toString():String {
			return value
		}
		
		public function toNumber():Number {
			return Number(value)
		}
		
		public function toArray():Array {
			return new JSONDecoder(value, true).getValue() as Array
		}
		
		public function get label():String {
			return value
		}
		
		public function applyData(data:Object):void {
			for (var p:String in data) {
				if (p == 'id' && !data[p]) continue;
				if (!data[p] || data[p] == "null") {
					if (this[p] is String) 		this[p] =  ''
					else if (this[p] is Number) this[p] =  0
					continue
				}
				
				// время храниться в цифровом виде (милисекунды)
				if (p.indexOf("_time") > 0 || p.indexOf("_date") > 0) {
					this[p] = new Date(Number(data[p]))
				}else
					this[p] = data[p]
				
				//if (p == 'id') isNew = false
			}
		}
	}
}